package com.example.a2209_mianshi.ui.login

import android.util.Log
import com.example.a2209_mianshi.flow_eventbus.utils.GSON
import com.example.a2209_mianshi.http.ClubService
import com.example.a2209_mianshi.ui.login.model.LoginMode
import com.example.a2209_mianshi.ui.login.model.LoginPostModel
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import javax.inject.Inject
class LoginRepository @Inject constructor() {
    @Inject
    lateinit var clubService: ClubService
    fun sendCodeLogin(operatorType:String,phone:String) = flow{
        delay(1_000)
        emit(clubService.sendLoginCode(operatorType,phone))
    }
    fun codeLogin(
        grantType:LoginMode,
        newPassword:String="",
        operatorType:Int=2,
        password:String="",
        smsCode:Int,
        userName:String
    ) = flow{
        val postModel = LoginPostModel(grantType.value,newPassword,operatorType,password,smsCode,userName)
        val body = RequestBody.create("application/json;charset=utf-8".toMediaType(),GSON.toJson(postModel))
        emit(clubService.codeLogin(body))
    }
}